package org.fuys.coder.domain.auth.service.handler.login.impl;

import org.fuys.coder.common.constants.RedisConstants;
import org.fuys.coder.common.entity.Pair;
import org.fuys.coder.common.exception.BusinessException;
import org.fuys.coder.infrastructure.util.RedisUtil;
import org.fuys.coder.domain.auth.model.req.LoginReq;
import org.fuys.coder.domain.auth.model.res.AuthFailedEnum;
import org.fuys.coder.domain.auth.model.vo.UserVo;
import org.fuys.coder.domain.auth.service.handler.login.LoginHandler;
import org.fuys.coder.domain.user.model.vo.operation.UserStopVO;
import org.fuys.coder.domain.user.service.repo.IUserService;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @projectName: fuys-low-coder
 * @package: org.fuys.coder.domain.auth.service.handler.login.impl
 * @className: FreezeCheckHandler
 * @author: WangJing
 * @description: 登录前的检查处理器 检查是否是封禁等
 * @date: 2024/8/9 17:38
 * @version: 1.0
 */
@Component
public class FreezeCheckHandler implements LoginHandler {

    @Resource
    private IUserService userService;

    @Override
    public Pair<Integer, UserVo> handle(LoginReq req) throws BusinessException {
        final UserStopVO stopUser = userService.getStopUser(req.getEmail());
        if(ObjectUtils.isEmpty(stopUser)){
            //放行
            return null;
        }
        if(stopUser.getOpen().before(new Date())){
            //放行
            userService.unFreezeUser(stopUser.getUserId(),req.getEmail());
            return null;
        }
        UserVo userVo=new UserVo();
        userVo.setOpenTime(stopUser.getOpen());
        return new Pair<Integer,UserVo>(AuthFailedEnum.LOGIN_USER_FREEZE.getIndex(),
                userVo);
    }
}
